'use strict';

angular.module('app')
  .factory('positionService', [
    '$http',
    '$state',
    '$q', /*解决回调地狱问题*/
    function($http, $state, $q) {
      return {
        query: function() {
          var defer = $q.defer(); //声明延后执行
          $http({
            method: 'GET',
            url: '/data/position.json?id=' + $state.params.id //获取json数据
          }).then(function(data, status, headers, config) {
            console.log('position success:' + angular.toJson(data.data));
            defer.resolve(data.data); //执行成功
          }).then(function(data, status, headers, config) {
            console.log('position error:' + data);
            defer.reject(); //执行失败
          });
          return defer.promise; //返回获取的数据
        }
      }
    }
  ])
  .factory('companyService', [
    '$http',
    '$state',
    '$q', /*解决回调地狱问题*/
    function($http, $state, $q) {
      return {
        query: function() {
          var defer = $q.defer(); //声明延后执行
          $http({
            method: 'GET',
            url: '/data/company.json?id=' + $state.params.id //获取json数据
          }).then(function(data, status, headers, config) {
            console.log('company success:' + angular.toJson(data.data));
            defer.resolve(data.data); //执行成功
          }).then(function(data, status, headers, config) {
            console.log('company error:' + data);
            defer.reject(); //执行失败
          });
          return defer.promise; //返回获取的数据
        }
      }
    }
  ])
  .controller('positionCtrl', [
    '$scope',
    '$http',
    'positionService',
    'companyService',
    'cache',
    '$state',
    '$log',
    function($scope, $http, positionService, companyService, cache, $state, $log) {
      /*cache.put('to', 'day'); //service
      cache.put('to', 'you'); //factory
      cache.remove('to');*/
      $scope.isLogin = cache.get('name') || '';
      $scope.message = $scope.isLogin ? "投个简历" : "去登录";
      var promise = positionService.query(); //同步调用，获取promise的接口
      promise.then(function(success) {
          $scope.position = success; //调用接口，如果获取数据成功 resolve()方法的实参
          if ($scope.position.posted) {
            $scope.message = "已投递";
          }
          console.log('position:$scope.position' + angular.toJson($scope.position));
        },
        function(error) {
          $scope.position = {
            error: 'data was not found...' //如果获取数据失败，调用reject()方法
          };
        });

      /*
      function getPosition() {
        var deferred = $q.defer();
        $http.get('/data/position.json?id=' + $state.params.id)
          .then(function(success) {
            $scope.position = success.data;
            deferred.resolve($scope.position);
          }, function(error) {
            deferred.reject(error);
          });
        return deferred.promise;
      }

      getPosition().then(function(obj) {
        console.log(obj);
      });
      */
      var promiseCom = companyService.query(); //同步调用，获取promise的接口
      promiseCom.then(function(success) {
          $scope.company = success; //调用接口，如果获取数据成功 resolve()方法的实参
          console.log('company:$scope.company' + angular.toJson($scope.company));
        },
        function(error) {
          $scope.company = {
            error: 'data was not found...' //如果获取数据失败，调用reject()方法
          };
        });
      $scope.go = function() {
        if ($scope.message !== "已投递") {
          if ($scope.isLogin) {
            $http.post('data/handle.json', {
              id: $scope.position.id,
            }).then(function(success) {
                $log.info(success);
                $scope.message = "已投递";
              },
              function(error) {});
          } else {
            $state.go('login');
          }
        }

      }
    }
  ]);
